import java.util.Iterator;
import java.util.Vector;

/*
 *  @author  Alpesh Mistry  <apm1dec@gmail.com>
 *  @author  Ankur Kothari  <a.kothari711@gmail.com>
 *  @author  Elman Mansimov <elman.mansimov@gmail.com>
 *  @author  Michelle Pan   <panmiche@gmail.com>
 *  @version 1.0
 *  @since   2012-03-03
 */

public class File extends SystemItem {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -6404012589396081856L;
	private Vector<String> contents = new Vector<String>();

	/**
	 * Initialize a File object with a name, parent directory
	 * and set its contents to an empty string.
	 * @param n String which is the name of File
	 * @param dir Directory which is the parent of Directory
	 */
	public File(String n, Directory dir) {
		
		super(n, dir);
		contents.add(0, null);
		contents.trimToSize();
		setContents("");
	}

	/**
	 * Set contents of File.
	 * @param text String which is contents of File
	 */
	public void setContents(String text) {

		contents.set(0, text);
	}
	
	/**
	 * Append text to contents of File after adding a new line.
	 * If File is empty make text to be new contents of it.
	 * @param text String which will be added to contents of File
	 */
	public void appendContents(String text) {

		if (!text.endsWith("\n"))
			text += "\n";

		contents.set(0, contents.firstElement() + text);
	}

	/**
	 * Return contents of File.
	 * @return String which is contents of File.
	 */

	public String getContents() {

		return contents.firstElement();
	}
	
	public Vector<String> getContentObject() {
		
		return contents;
	}

	public String printContents(boolean recurse, String path) {

		return this.getFullPath() + "\n";
	}

	@SuppressWarnings("unchecked")
	public void linkContents(Object newContents) {
		// TODO Auto-generated method stub
		contents = (Vector<String>) newContents;
	}

	public void nullifyLinks(){
		
		Iterator<SystemItem> itr = this.getLinksMade().iterator();
		SystemItem temp = null;

		while (itr.hasNext()) {
			
			temp = itr.next();
			((File) temp).linkContents(null);
		}
	}
	
	public File copy(Directory parentGiven){

		File tempCopy = new File(this.getName(), parentGiven); 

		if (this.getLink()) {
			tempCopy.linkContents(this.getContentObject()); 
		} else {
			tempCopy.setContents(this.getContents());
		}
		return tempCopy;
	}

	@Override
	public void eraseContents() {
		// TODO Auto-generated method stub
		contents = null;
	}
}